//
//  MessageCell.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/12.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "MessageCell.h"
#import "TimeTools.h"
#import "TopicDetailViewController.h"

#define  LIGHT_BLUE_COLOR [UIColor colorWithRed:127/255.0 green:181/255.0 blue:231/255.0 alpha:1.0]

@implementation MessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.width/2;
    self.headImageView.layer.masksToBounds = self.headImageView.frame.size.width/2;

    YYLabel *label = [[YYLabel alloc] init];
    label.frame = CGRectMake(CGRectGetMaxX(self.headImageView.frame)+8,CGRectGetMaxY(self.nameLabel.frame)+3, SCREEN_WIDTH-20*2-self.headImageView.frame.size.width, 40);
    label.textAlignment = NSTextAlignmentCenter;
    label.textVerticalAlignment = YYTextVerticalAlignmentTop;
    label.numberOfLines = 2;
    [self addSubview:label];
    self.contentLabel = label;
}

- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - 业务逻辑
-(void)setAvatar:(NSString *)avatar_url name:(NSString *)name createdDate:(NSString *)createdDate title:(NSString *)title content:(NSString *)content isReply:(BOOL)isreply{
    NSString *insert = nil;
    if (isreply) {
        insert = @"下回复了你的评论：";
    }
    else {
        insert = @"中回复了你：";
    }
    NSString *text = [NSString stringWithFormat:@"在话题【%@】%@%@", title, insert, content];
    
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:text];
    
    /**
     *  设置整段文本size
     */
    one.yy_font = [UIFont boldSystemFontOfSize:15];
    /**
     *  获得range， 只设置标记文字的size、下划线
     */
    NSRange range = [text rangeOfString:[NSString stringWithFormat:@"【%@】",title]];
    [one yy_setTextUnderline:[YYTextDecoration decorationWithStyle:YYTextLineStyleNone] range:range];
    [one yy_setFont:[UIFont boldSystemFontOfSize:15] range:range];
    
    /**
     *  被标记的文字颜色
     */
    UIColor *textColor =LIGHT_BLUE_COLOR;
    /**
     *  标记文字点击事件
     */
    [one yy_setTextHighlightRange:range color:textColor backgroundColor:nil tapAction:nil];
    self.contentLabel.attributedText = one;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:avatar_url] placeholderImage:[UIImage imageNamed:@"头像"]];
    self.nameLabel.text = name;
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    [fmt setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [fmt setDateFormat:@"yyyy-MM-dd'T'HH:mm:sszz"];
    NSDate *d = [fmt dateFromString:createdDate];
    self.timeLabel.text = [TimeTools getYearMonthDayHourMinWithDate:d];
}

@end
